package com.miaostars.apakey.events;

import com.miaostars.apakey.Apakey;
import com.miaostars.apakey.checks.AutoClickChecks;
import com.miaostars.apakey.checks.CombatChecks;
import com.miaostars.apakey.utils.AlertManager;
import com.miaostars.apakey.utils.PlayerData;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;

public class CombatListener implements Listener {
    
    private final Apakey plugin;
    private final CombatChecks combatChecks;
    private final AutoClickChecks autoClickChecks;
    private final AlertManager alertManager;
    
    public CombatListener(Apakey plugin) {
        this.plugin = plugin;
        this.combatChecks = new CombatChecks(plugin);
        this.autoClickChecks = new AutoClickChecks(plugin);
        this.alertManager = plugin.getAlertManager();
    }
    
    @EventHandler
    public void onDamage(EntityDamageByEntityEvent event) {
        if (!(event.getDamager() instanceof Player)) {
            return;
        }
        
        Player player = (Player) event.getDamager();
        PlayerData data = PlayerData.get(player);
        
        // Record attack for CPS check
        data.getClicks().add(System.currentTimeMillis());
        
        // Check KillAura (CPS)
        if (combatChecks.checkKillAura(player, data)) {
            alertManager.alert(player, "KillAura", "A");
            event.setCancelled(true);
            return;
        }
        
        // Check AutoClick
        if (autoClickChecks.checkAutoClick(player, data)) {
            alertManager.alert(player, "AutoClick", "A");
            event.setCancelled(true);
            return;
        }
        
        // Check Reach
        if (event.getEntity() instanceof Player) {
            double distance = player.getLocation().distance(event.getEntity().getLocation());
            if (combatChecks.checkReach(player, data, distance)) {
                alertManager.alert(player, "Reach", "A");
                event.setCancelled(true);
            }
        }
    }
    
    @EventHandler
    public void onInteract(PlayerInteractEvent event) {
        // Record right clicks for CPS check
        if (event.getAction().toString().contains("RIGHT")) {
            Player player = event.getPlayer();
            PlayerData data = PlayerData.get(player);
            data.getClicks().add(System.currentTimeMillis());
            
            // Check KillAura (CPS)
            if (combatChecks.checkKillAura(player, data)) {
                alertManager.alert(player, "KillAura", "B");
                event.setCancelled(true);
                return;
            }
            
            // Check AutoClick
            if (autoClickChecks.checkAutoClick(player, data)) {
                alertManager.alert(player, "AutoClick", "B");
                event.setCancelled(true);
            }
        }
    }
}
